#ifndef __wDXSOUND_H__
#define __wDXSOUND_H__

#ifndef __wMISC_H__
	#error wDxSound.h requires wMisc.h to be included first
#endif

#include <dsound.h>
#pragma comment(lib, "dsound.lib")

class wDxSoundEnumerate
{
public:
	GUID GetAudioGUID(LPCTSTR lpszAudioName, BOOL bLike, BOOL bAudioOut = TRUE)
	{
		chASSERT(!chIS_EMPTY(lpszAudioName));
		m_lpszAudioName = lpszAudioName;
		m_bMatchLike = bLike;
		m_guidAudioID = GUID_NULL;
		if(bAudioOut)
			DirectSoundEnumerate(Dx_CB_Name2GUID, this);
		else
			DirectSoundCaptureEnumerate(Dx_CB_Name2GUID, this);
		return m_guidAudioID;
	}
	LPCTSTR GetAudioName(GUID guid, BOOL bAudioOut = TRUE)
	{
		chASSERT(guid != GUID_NULL);
		m_guidAudioID = guid;
		m_szAudioName[0] = 0;
		if(bAudioOut)
			DirectSoundEnumerate(Dx_CB_GUID2Name, this);
		else
			DirectSoundCaptureEnumerate(Dx_CB_GUID2Name, this);
		return m_szAudioName;
	}
private:
	static BOOL CALLBACK Dx_CB_Name2GUID(LPGUID  lpGuid, LPCTSTR  lpcstrDescription, LPCTSTR  lpcstrModule, LPVOID  lpContext)
	{
		wDxSoundEnumerate* pThis = (wDxSoundEnumerate*)lpContext;
		
		if(lpGuid != NULL)
		{
			if(pThis->m_bMatchLike && chFind<TCHAR>(lpcstrDescription, pThis->m_lpszAudioName) != NULL)
			{
				pThis->m_guidAudioID = *lpGuid;
				return FALSE;
			}
			else if(!pThis->m_bMatchLike && chTEXT_SAME(lpcstrDescription, pThis->m_lpszAudioName))
			{
				pThis->m_guidAudioID = *lpGuid;
				return FALSE;
			}
		}
		return TRUE;
	}
	static BOOL CALLBACK Dx_CB_GUID2Name(LPGUID  lpGuid, LPCTSTR  lpcstrDescription, LPCTSTR  lpcstrModule, LPVOID  lpContext)
	{
		wDxSoundEnumerate* pThis = (wDxSoundEnumerate*)lpContext;
		
		if(lpGuid != NULL && pThis->m_guidAudioID == *lpGuid)
		{
			chStrcpy<TCHAR>(pThis->m_szAudioName, lpcstrDescription);
			return FALSE;
		}
		return TRUE;
	}
public:
	wDxSoundEnumerate()
	{
		m_lpszAudioName		= NULL;
		m_bMatchLike		= FALSE;
		m_guidAudioID		= GUID_NULL;
		m_szAudioName[0]	= 0;
	}
private:
	BOOL		m_bMatchLike;
	GUID		m_guidAudioID;
	LPCTSTR		m_lpszAudioName;
	TCHAR		m_szAudioName[MAXPNAMELEN];
};

#endif //__wDXSOUND_H__